package com.clickpaas.workinghours.common.Interceptor;

import com.bizcloud.ipaas.tbba4bf83484b4ed2aacba581fbfeac4d.  d20210220155135.auth.extension.AuthConfig;
import com.clickpaas.workinghours.config.AutoLoadConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @company <上海爱湃斯科技有限公司>
 * @email <zhihui.peng@clickpaas.com>
 * @author huiye
 * @decription: 拦截器验证用户登陆token
 * @date 2020/12/27 1:22 下午
 */
@Slf4j
public class InitVerInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURL = request.getRequestURL().toString();
        if (requestURL.endsWith("Tenant")){
            iniTenant(AutoLoadConfig.ACCESSKEY_ID_TENANT,AutoLoadConfig.ACCESSKEY_SECRET_TENANT);
        }else {
            String token = request.getHeader("tg-token");
            init(AutoLoadConfig.ACCESSKEY_ID,AutoLoadConfig.ACCESSKEY_SECRET,token);
        }
        return true;
    }

    public void init( String accesskeyId, String accesskeySecret, String token) {
        AuthConfig authConfig = new AuthConfig(accesskeyId, accesskeySecret,token);
        authConfig.initAuth();
    }

    public void iniTenant( String accesskeyId, String accesskeySecret) {
        AuthConfig authConfig = new AuthConfig(accesskeyId, accesskeySecret);
        authConfig.initAuth();
    }

}
